package com.pro.util.generator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.annotation.processing.Filer;

public class MapperReader {

	private String src;
	private Table table;

	private String daoPackage;
	private String classType;

	public String getClassType() {
		return classType;
	}

	public void setClassType(String classType) {
		this.classType = classType;
	}

	public String getDaoPackage() {
		return daoPackage;
	}

	public void setDaoPackage(String daoPackage) {
		this.daoPackage = daoPackage;
	}

	public MapperReader(String src, Table table) {
		super();
		this.src = src;
		this.table = table;
	}

	private String beanPackage;

	public String getBeanPackage() {
		return beanPackage;
	}

	public void setBeanPackage(String beanPackage) {
		this.beanPackage = beanPackage;
	}

	public String readFileString() throws IOException {
		StringBuffer sb = new StringBuffer();
		FileReader fr = new FileReader(new File(src));
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		String beanPackage = this.getBeanPackage() != null ? this.getBeanPackage()
				: (table.getBeanPackage() != null ? table.getBeanPackage() : "");
		String daoPackage = this.getDaoPackage() != null ? this.getDaoPackage()
				: (table.getDaoPackage() != null ? table.getDaoPackage() : "");
		while ((line = br.readLine()) != null) {
			System.out.println("test");
			String source = line.replaceAll("\\$\\{UtableName\\}", table.getuTableName())
					.replaceAll("\\$\\{tableName\\}", table.getTableName())
					.replaceAll("\\$\\{basePackage\\}", table.getPackageName()!=null?table.getPackageName():"")
					.replaceAll("\\$\\{daoPackage\\}", daoPackage)
					.replaceAll("\\$\\{classType\\}", this.getClassType() != null ? this.getClassType() : "class")
					.replaceAll("\\$\\{beanPackage\\}", beanPackage);
			if (source.contains("${objectSet}")) {
				StringBuffer templateSb = new StringBuffer();
				String template = "<if test=\"${fieldName} != null and ${fieldName} != ''\">${fieldName}=#{${fieldName},jdbcType=${fieldType}},</if>";
				for (ColumnBean col : table.getColumnList()) {
					templateSb.append("\t" + template.replaceAll("\\$\\{fieldName\\}", col.getName())
							.replaceAll("\\$\\{fieldType\\}", col.getJdbcType()) + "\r\n");

				}
				sb.append(templateSb);
			}
			source = source.replaceAll("\\$\\{objectSet\\}", "");
			if (source.contains("${searchCondition}")) {
				StringBuffer templateSb = new StringBuffer();
				String template = "<if test=\"${fieldName} != null and ${fieldName} !=''\"> and ${fieldName} =#{fieldName} </if>";
				for (ColumnBean col : table.getColumnList()) {
					templateSb.append("\t" + template.replaceAll("\\$\\{fieldName\\}", col.getName()) + "\r\n");
				}
				sb.append(templateSb);
			}

			source = source.replaceAll("\\$\\{searchCondition\\}", "");
			sb.append(source);
			sb.append("\r\n");
		}
		return sb.toString();
	}
}
